package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatFriendVO;
import com.zenithmind.chat.service.ChatFriendService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 好友管理控制器 - 遵循单一职责原则
 * 只负责好友关系的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/friends")
@RequiredArgsConstructor
@Validated
@Tag(name = "好友管理", description = "好友关系相关接口")
public class ChatFriendController {

    private final ChatFriendService chatFriendService;

    @GetMapping("/page")
    @Operation(summary = "分页查询好友列表", description = "分页查询当前用户的好友列表")
    public Result<PageResult<ChatFriendVO>> getFriendPage(
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
        String userId = UserContext.getUserId();
        PageResult<ChatFriendVO> result = chatFriendService.getFriendPage(userId, keyword, status, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询好友列表", description = "查询当前用户的所有好友")
    public Result<List<ChatFriendVO>> getFriendList(
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.getUserId();
        List<ChatFriendVO> friends = chatFriendService.getUserFriends(userId, keyword, status);
        return Result.success(friends);
    }

    @GetMapping("/{friendId}")
    @Operation(summary = "查询好友详情", description = "根据好友ID查询好友详细信息")
    public Result<ChatFriendVO> getFriendById(
            @Parameter(description = "好友ID") @PathVariable @NotBlank String friendId) {
        String userId = UserContext.getUserId();
        ChatFriendVO friend = chatFriendService.getFriendById(friendId, userId);
        return Result.success(friend);
    }

    @DeleteMapping("/{friendId}")
    @Operation(summary = "删除好友", description = "删除指定的好友关系")
    public Result<Boolean> deleteFriend(
            @Parameter(description = "好友ID") @PathVariable @NotBlank String friendId) {
        String userId = UserContext.getUserId();
        Boolean result = chatFriendService.deleteFriend(friendId, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/remark")
    @Operation(summary = "修改好友备注", description = "修改指定好友的备注名称")
    public Result<Boolean> updateFriendRemark(
            @Parameter(description = "好友ID") @PathVariable @NotBlank String friendId,
            @Parameter(description = "备注名称") @RequestParam String remark) {
        String userId = UserContext.getUserId();
        Boolean result = chatFriendService.updateFriendRemark(friendId, remark, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/status")
    @Operation(summary = "修改好友状态", description = "修改指定好友的状态")
    public Result<Boolean> updateFriendStatus(
            @Parameter(description = "好友ID") @PathVariable @NotBlank String friendId,
            @Parameter(description = "状态") @RequestParam Integer status) {
        String userId = UserContext.getUserId();
        Boolean result = chatFriendService.updateFriendStatus(friendId, status, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除好友", description = "批量删除多个好友关系")
    public Result<Boolean> batchDeleteFriends(@RequestBody @NotEmpty List<String> friendIds) {
        String userId = UserContext.getUserId();
        Boolean result = chatFriendService.batchDeleteFriends(friendIds, userId);
        return Result.success(result);
    }

    @GetMapping("/count")
    @Operation(summary = "统计好友数量", description = "统计当前用户的好友数量")
    public Result<Integer> countFriends(
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.getUserId();
        Integer count = chatFriendService.countFriends(userId, status);
        return Result.success(count);
    }

    @GetMapping("/check/{targetUserId}")
    @Operation(summary = "检查好友关系", description = "检查与指定用户是否为好友关系")
    public Result<Boolean> checkFriendship(
            @Parameter(description = "目标用户ID") @PathVariable @NotBlank String targetUserId) {
        String userId = UserContext.getUserId();
        Boolean isFriend = chatFriendService.checkFriendship(userId, targetUserId);
        return Result.success(isFriend);
    }

    @GetMapping("/mutual/{targetUserId}")
    @Operation(summary = "查询共同好友", description = "查询与指定用户的共同好友")
    public Result<List<ChatFriendVO>> getMutualFriends(
            @Parameter(description = "目标用户ID") @PathVariable @NotBlank String targetUserId) {
        String userId = UserContext.getUserId();
        List<ChatFriendVO> mutualFriends = chatFriendService.getMutualFriends(userId, targetUserId);
        return Result.success(mutualFriends);
    }
}
